// @ts-ignore
import * as Joi from "@hapi/joi"
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { CoffeesModule } from './coffees/coffees.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule } from '@nestjs/config';
import { CodeModule } from './code/code.module';
import { UploadModule } from './upload/upload.module';
import { GuardModule } from './guard/guard.module';

@Module({
  imports: [
    ConfigModule.forRoot({
      // 配置配置文件路径
      envFilePath: '.env',
      // 配置配置文件的变量验证
      validationSchema: Joi.object({
        DATABASE_HOST: Joi.required(),
        DATABASE_PORT: Joi.number().default(5432)
      })
    }),
    CoffeesModule,
    TypeOrmModule.forRoot({
      type: 'mysql', //数据库类型
      host: process.env.DATABASE_HOST, //主机
      port: +process.env.DATABASE_PORT, //端口
      username: process.env.DATABASE_USER, //用户名
      password: process.env.DATABASE_PASSWORD, //密码
      database: process.env.DATABASE_NAME, //数据库名
      autoLoadEntities: true, //自动加载模块
      synchronize: true // 同步：确保TypeORM实体每次运行应用程序时都会与数据库同步 !生产环境中时必须禁用! 会根据Entity生成mysql表格
    }),
    CodeModule,
    UploadModule,
    GuardModule
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
